Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

oro-functions

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

oro-functions

Class Ofn extended from Ofn-Client with more helper static functions: URLs, Files, Operating System.

  • 1.3.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by400%
Maintainers
1
Weekly downloads
 
Created
Source

Oro Functions

Overview

By default, Ofn, it's a simple class with helper static functions.

This package ( oro-functions ) is divided from oro-functions-client to allow using that functions in js-client frameworks like Vuejs.

Class oro-functions is extended from oro-functions-client. If you want to know all fns, read oro-functions-client.

Installation

npm install oro-functions

Example:

const Ofn = require( 'oro-functions' );

// ts
import Ofn from 'oro-functions';

Ofn.type( [ 1, 2, 3 ] );
// 'array'

Methods


Oro Functions Client

In Ofn there are all functions of Oro Functions Client.


Extended Functions

URLs


Ofn.jwkTokenDecode()
jwkTokenDecode( token: string ) => string;
Ofn.jwkTokenDecode( 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoib3JvcGVzYSIsImlhdCI6MTYyOTcxMzM2MywiZXhwIjoxNjI5NzIwNTYzfQ.2zL8FzvFQCtuqi0fFoby4QVCXSi2pWNS3bzCU53Vd4M' );
// -> '{"user":"oropesa","iat":1629713363,"exp":1629720563}'

Crypto


Ofn.cryptoGenerateKeyPair()
Ofn.cryptoGenerateKeyPair(
  passphrase?: string,
  options?: CryptoKeyPairOptions
) => Promise<CryptoKeyPairResponse>

interface CryptoKeyPairOptions {
  type?: string;
  modulusLength?: number;
  publicKeyEncodingType?: string;
  publicKeyEncodingFormat?: string;
  privateKeyEncodingType?: string;
  privateKeyEncodingFormat?: string;
  privateKeyEncodingCipher?: string;
}

type CryptoKeyPairResponse = SResponse<
  CryptoKeyPairObject,  // as SResponseOK
  CryptoKeyPairError    // as SResponseKO
>;

interface SResponseOK {
  status: true,
  passphrase: string;
  publicKey: string;
  privateKey: string;
}

interface SResponseKO {
  status: false,
  error: {
    msg: string;
    err: Error;
  }
}

interface CryptoKeyPairObject {
  passphrase: string;
  publicKey: string;
  privateKey: string;
}

interface CryptoKeyPairError {
  msg: string;
  err: Error;
}

await Ofn.cryptoGenerateKeyPair( 'example' )
// {
//   passphrase: 'example',
//   publicKey: '-----BEGIN RSA PUBLIC KEY-----',
//   privateKey: '-----BEGIN RSA PRIVATE KEY-----'
// }
  • Default options
options = {
    type: 'rsa', // 'rsa', 'rsa-pss', 'dsa', 'ec', 'ed25519', 'ed448', 'x25519', 'x448', 'dh'.
    modulusLength: 4096,
    publicKeyEncodingType: 'spki', // 'pkcs1' (RSA only) or 'spki'.
    publicKeyEncodingFormat: 'pem', // 'pem', 'der', or 'jwk'.
    privateKeyEncodingType: 'pkcs8', // 'pkcs1' (RSA only), 'pkcs8' or 'sec1' (EC only).
    privateKeyEncodingFormat: 'pem', // 'pem', 'der', or 'jwk'.
    privateKeyEncodingCipher: 'aes-256-cbc', // 'aes-256-cbc', 'des-cbc-sha', 'rc4-128-md5', ...
}

Files


await Ofn.obtainOConfig()
Ofn.obtainOConfigSync()
await Ofn.obtainOConfig<T extends object = OConfigDefaultParams>
    ( args?: OConfigArgs ) => Promise<OConfigResponse<T>>;

Ofn.obtainOConfigSync<T extends object = OConfigDefaultParams>
    ( args?: OConfigArgs ) => OConfigResponse<T>;

interface OConfigArgs {
  file?: string;
  deep?: number;
  defaultParams?: string[];
  extraParams?: string[];
}

interface OConfigDefaultParams {
  environment: string;
  projectname: string;
  projectserver: string;
}

type OConfigResponse<T extends object = OConfigDefaultParams> = 
    SResponse<
      OConfigObject<T>, // as SResponseOK
      OConfigError      // as SResponseKO
    >;

interface SResponseOK {
  status: true,
  config: T;
}

interface SResponseKO {
  status: false,
  error: {
    msg: string;
    args?: OConfigArgs;
  }
}

interface OConfigObject<T extends object = OConfigDefaultParams> {
  config: T;
}

interface OConfigError {
  msg: string;
  args?: OConfigArgs;
}

Put yourself in this situation, you work with Git and you create modules or projects that need the common file .env to use custom variables, with data about, i.e., connect to dbs, users-passwords, dev-pro or other envs, custom configs.

Then, you have something like:

- parent-folder/
  ├─ project-1/  #git-project-1
  │   └...
  ├─ project-2/  #git-project-2
  │   └...
  └...

Instead of create global variables in the system, or duplicate the file .env, you can centralize all data in oro-config.json.

This file, oro-config.json, could be in the same project and in the parent folders too, in such a way that the final json is merged and overwritten from the parent folders to the project.

So, using this example, you have:

- parent-folder/
  ├─ project-1/
  │   ├─ oro-config.json #json of project 1
  │   └...
  ├─ project-2/
  │   ├─ oro-config.json #json of project 2
  │   └...
  ├─ oro-config.json #json of parent-folder
  └...

Continuing, the oro-config.json files have the next data:

// oro-config.json of Parent Folder
{
  "environment": "dev",
  "projectserver": "laptop-dev",
  "nuxt": {
    "server": "localhost",
    "defaultRole": "default"
  }
}

// oro-config.json of Project 1
{
  "projectname": "project-1"
}

// oro-config.json of Project 2
{
  "projectname": "project-2",
  "nuxt": {
    "pageName": "The Project 2",
    "defaultRole": "client"
  }
}

Finally, the json result is:

/* Inside project 1 */

let obtainConfig = await Ofn.obtainOConfig( { deep: 1 } );
// {
//   status: true,
//   config: {
//     environment: 'dev',
//     projectserver: 'laptop-dev',
//     projectname: 'project-1',
//     nuxt: {
//       server: 'localhost',
//       defaultRole: 'default'
//     }
//   }
// }

/* Inside project 2 */

let obtainConfig = await Ofn.obtainOConfig( { deep: 1 } );
// {
//   status: true,
//   config: {
//     environment: 'dev',
//     projectserver: 'laptop-dev',
//     projectname: 'project-2',
//     nuxt: {
//       server: 'localhost',
//       pageName: 'The Project 2',
//       defaultRole: 'client'
//     }
//   }
// }
  • Default args
let args = {
  file: 'oro-config.json',
  deep: 0,
  defaultParams: [ 'environment', 'projectname', 'projectserver' ],
  extraParams: []
}

By default, there are 3 params that are required in json: environment, projectname, projectserver.

So, if there are missing, the response of Ofn.obtainOConfig() is { status: false, ... }.

Alternatively, you can change defaultParams, or you can add extraParams to be required.

On the other hand, you can change de file name to search, so instead of oro-config.json, you can use custom.json.

Finally, you choose the deep from parents to look for.


await Ofn.getFileJsonRecursively()
Ofn.getFileJsonRecursivelySync()
await Ofn.getFileJsonRecursively<T>
    ( filenameOrPath: string, parentDeep?: number ) => Promise<T>;

Ofn.getFileJsonRecursivelySync<T>
    ( filenameOrPath: string, parentDeep?: number ) => T;

Having this case:

- main/
  ├─ folder/
  │   ├─ subfolder/
  │   │   ├─ index.js 
  │   │   └─ custom.json 
  │   └ custom.json 
  └ custom.json 

The final json is merged and overwritten from the parents to the project file.

// in index.js
let custom = await Ofn.getFileJsonRecursively( 'custom.json', 2 );
// { ... } 

await Ofn.globFiles()
await Ofn.globFiles = (
  folderPath: string | string[],
  globArgs?: GlobFilesOptions,
) => Promise<string[]>;
await Ofn.globFiles( `folder/*` );
// [
//   `folder/example.txt`,
//   ...
// ]
  • Default args

This function is a wrapper of fast-glob with default args. so GlobFilesOptions are the same as fast-glob Options

// default
globArgs = {
    dot: true,
    unique: true,
    onlyFiles: true,
    ignore: [ "node_modules/**", ".zero/**" ]
}

await Ofn.folderIsEmpty()
await Ofn.folderIsEmpty = (
  folderPath: string,
  globArgs?: GlobFilesOptions,
) => Promise<boolean>;
await Ofn.folderIsEmpty( `folder/` ); // false
  • Default args

This function is a wrapper of fast-glob with default args. so GlobFilesOptions are the same as fast-glob Options

// default
globArgs = {
    dot: true,
    unique: true,
    onlyFiles: true,
    ignore: [ "node_modules/**", ".zero/**" ]
}

await Ofn.pathIsFolder()
await Ofn.pathIsFolder = ( folderPath: string ) => Promise<boolean>;
await Ofn.pathIsFolder( `folder` ); 
// -> true

await Ofn.zipFolder()
await Ofn.zipFolder = ( folderPath: string, zipPath?: string ) => Promise<ZipFolderResponse>;

type ZipFolderResponse = SResponse<
  ZipFolderObject, // as SResponseOK
  ZipFolderError   // as SResponseKO
>;

interface SResponseOK {
  status: true,
  zipPath: string;
}

interface SResponseKO {
  status: false,
  error: {
    msg: string;
    folderPath?: string;
    zipPath?: string;
  }
}

interface ZipFolderObject {
  zipPath: string;
}

interface ZipFolderError {
  msg: string;
  folderPath?: string;
  zipPath?: string;
}
await Ofn.zipFolder( `folder`, 'folder.zip' ); 
// -> { status: true, zipPath: 'folder.zip' }

Operating System

Ofn.osPlatform()
Ofn.osPlatform = () => NodeJS.Platform;
Ofn.osPlatform(); 
// -> 'win32' || 'darwin' || 'linux' || ...

Ofn.osIsWindows()
Ofn.osIsWindows = () => boolean;
Ofn.osIsWindows();
// -> true

Ofn.osIsMac()
Ofn.osIsMac = () => boolean;
Ofn.osIsMac();
// -> false

Ofn.osIsLinux()
Ofn.osIsLinux = () => boolean;
Ofn.osIsLinux();
// -> true

Ports


await Ofn.isPortFree()
await Ofn.isPortFree = ( port: number ) => Promise<IsPortFreeResponse>;

type IsPortFreeResponse = SResponse<
  PortFreeObject, // as SResponseOK
  IsPortFreeError // as SResponseKO
>;

interface SResponseOK {
  status: true,
  port: number;
}

interface SResponseKO {
  status: false,
  error: {
    msg: string;
    port: number;
  }
}

interface PortFreeObject {
  port: number;
}

interface IsPortFreeError {
  msg: string;
  port: number;
}
await Ofn.isPortFree( 3000 ); 
// -> { status: true, port: 3000 }

await Ofn.getPortFree()
await Ofn.getPortFree = ( portStart?: number | number[], portEnd?: number ) 
    => Promise<GetPortFreeResponse>;

type GetPortFreeResponse = SResponse<
  PortFreeObject,  // as SResponseOK
  GetPortFreeError // as SResponseKO
>;

interface SResponseOK {
  status: true,
  port: number;
}

interface SResponseKO {
  status: false,
  error: {
    msg: string;
    port?: number;
    opts?: {
      random?: boolean;
      port?: number;
      ports?: number[];
      portRange?: number[];
    };
    err?: any;
  }
}

interface PortFreeObject {
  port: number;
}

interface GetPortFreeError {
  msg: string;
  port?: number;
  opts?: {
    random?: boolean;
    port?: number;
    ports?: number[];
    portRange?: number[];
  };
  err?: any;
}
await Ofn.getPortFree(); 
// -> { status: true, port: 60247 } #random

await Ofn.getPortFree( 3000 ); 
// -> { status: true, port: 3000 } #if not allowed, return random

await Ofn.getPortFree( [ 3000, 3001, 3002 ] ); 
// -> { status: true, port: 3000 }
// -> { status: false, error: { msg: 'No available ports in array [ 3000, 3001, 3002 ]' } }

await Ofn.getPortFree( 3000, 3100 ); 
// -> { status: true, port: 3000 }
// -> { status: false, error: { msg: 'No available ports in range 3000-3100' } }

Console


Ofn.processWrite()
Ofn.processWrite(
  strOrObject: string | ProcessWriteObject,
  color?: string,
  bg?: string
) => string;

type ProcessWriteObject =
    | { s?: string, c?: string, b?: string }
    | { str?: string, cl?: string, bg?: string }
    | { string?: string, color?: string, background?: string }
Ofn.processWrite( 'info', 'blue' );
Ofn.processWrite( ' Doing some stuff... ' );

Ofn.processWrite( { s: 'Error!', c: 'red', b: 'redlight' } );
Ofn.processWrite( '\n' );
  • Example:

Example Console - Process Write

Note: first param could be a string or an object

  • Allowed Object:

    • s, str, or string
    • c, cl, or color
    • b, bg, or background
  • Allowed Colors and Background:

    • gray
    • red
    • green
    • white
    • yellow
    • blue
    • redlight
    • bluelight

Ofn.processWrites()
Ofn.processWrites( arr: Array<string | ProcessWriteObject> ) => string;

type ProcessWriteObject =
    | { s?: string, c?: string, b?: string }
    | { str?: string, cl?: string, bg?: string }
    | { string?: string, color?: string, background?: string }
Ofn.processWrites( [ 
  { s: ' info ', c: 'blue', b: 'bluelight' }, 
  ' Doing some stuff... ',
  { s: 'Error!', c: 'red', b: 'redlight' }, 
  '\n'
] );
  • Example:

Example Console - Process Writes

Keywords

FAQs

Package last updated on 19 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc